package cn.bugstack.gateway.bind;


import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.dubbo.rpc.service.GenericService;

import java.lang.reflect.Method;

/*
 * 一般代理
 */
public class GenericReferenceProxy implements MethodInterceptor {
    private final GenericService genericService;// 调用服务，替代所有接口引用
    private final String methodName; // 调用方法

    public GenericReferenceProxy(GenericService genericService, String methodName) {
        this.genericService = genericService;
        this.methodName = methodName;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        Class<?>[] parameterTypes = method.getParameterTypes(); // 通过方法对象，得到方法的参数
        String[] parameters = new String[parameterTypes.length];
        for (int i = 0; i < parameterTypes.length; i++) {
            parameters[i] = parameterTypes[i].getName(); // 获得所有方法名称
        }
        return genericService.$invoke(methodName, parameters, args); //通过代理对象调用方法
    }
}
